# Maintainer: Caleb Maclennan <caleb@alerque.com>

pkgbase=lua-luarepl
_rockname=${pkgbase#lua-}
pkgname=("$pkgbase" "lua51-$_rockname" "lua52-$_rockname" "lua53-$_rockname")
pkgver=0.10
_rockrel=1
pkgrel=2
pkgdesc='A reusable REPL component for Lua, written in Lua'
arch=(any)
url="https://github.com/hoelzro/${pkgbase/-lua/-}"
license=(MIT)
depends=(bash)
makedepends=(lua
             lua51
             lua52
             lua53
             luarocks)
checkdepends=(perl
              lua-testmore)
optdepends=('rlwrap: Enable basic line editing')
_archive="${pkgbase/-lua/-}-$pkgver"
source=("$url/archive/$pkgver/$_archive.tar.gz")
sha256sums=('55ba9f032bb4eb0e2e93dc66a368549bcf1a915bdd9f9a467eb778c3133c6373')

check() {
	cd "$_archive"
	make test
}

_package() {
	cd "$_archive"
	depends+=("${pkgname%-*}")
	provides=("${pkgname/-lua/-}=$pkgver")
	replaces=("${pkgname/-lua/-}")
	optdepends+=("${pkgname%-*}-filesystem: Enable filename_completion plugin"
	             "${pkgname%-*}-linenoise: Enable advanced line editing, history, and tab completion")
	luarocks --lua-version="$1" --tree="$pkgdir/usr/" \
		make --deps-mode=none --no-manifest "$_rockname-$pkgver-$_rockrel.rockspec"
	sed -i -e "s#$pkgdir##" "$pkgdir/usr/bin/rep.lua"
	sed -i -e "1s/lua/${pkgname%-*}/" "$pkgdir/usr/lib/luarocks/rocks-$1/$_rockname/$pkgver-$_rockrel/bin/rep.lua"
	install -Dm0644 -t "$pkgdir/usr/share/licenses/$pkgname/" COPYING
	[[ ${pkgname%-*} == lua ]] || mv "$pkgdir"/usr/bin/rep.{lua,${pkgname%-*}}
}

package_lua-luarepl() {
	_package 5.4
}

package_lua51-luarepl() {
	_package 5.1
}

package_lua52-luarepl() {
	_package 5.2
}

package_lua53-luarepl() {
	_package 5.3
}
